package org.nowcoder.leetcode.hashTable;

import java.util.*;

/**
 * Title  : 49. Group Anagrams
 * Source : https://leetcode.cn/problems/group-anagrams/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-01
 */
public class LeetCode_49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> words = new HashMap<>();
        for (String str : strs) {
            char[] cs = str.toCharArray();
            Arrays.sort(cs);
            String s = String.valueOf(cs);
            List<String> list = words.getOrDefault(s, new ArrayList<>());
            list.add(str);
            words.put(s, list);
        }
        return new ArrayList<>(words.values());
    }
}
